package com.example.activiti;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.impl.HistoryServiceImpl;
import org.activiti.engine.impl.RuntimeServiceImpl;
import org.activiti.engine.impl.TaskServiceImpl;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
public class ActivitiTaskVar {
    @Resource
    RuntimeServiceImpl runtimeService;
    @Resource
    TaskServiceImpl taskService;
    @Resource
    HistoryServiceImpl historyService;
    @Resource
    RepositoryService repositoryService;

    @Test
    void startProcess() {
        Map<String, Object> paramMap = new HashMap<>();
        // 设置不同节点办理人
        paramMap.put("user1","user");

        // 开始任务，生成流程实例
        runtimeService.startProcessInstanceByKey("holiday",paramMap);
    }

    @Test
    void taskQuery() {
        // 任务查询
        List<Task> taskList = taskService.createTaskQuery().taskAssignee("user").list();
        taskList.forEach(task -> {
            System.out.println("任务名称" + task.getName());
            System.out.println("拥有者" + task.getOwner());
            System.out.println("任务描述" + task.getDescription());
            System.out.println("任务id" + task.getId());

            System.out.println("-------------");
        });
    }

    @Test
    void completeTask() {
        // 完成任务
        // 1.查询请假流程任务（通过流程实例与当前处理者查询）
        Task task = taskService.createTaskQuery()
                .taskAssignee("money")
                .singleResult();
        // 通过流程设置变量
        String processInstanceId = task.getProcessInstanceId();
        runtimeService.setVariable(processInstanceId,"user2","user3");
        // 通过任务id变量
        taskService.setVariable(task.getId(),"user1","dept");
        //taskService.setVariableLocal();

        taskService.complete(task.getId());

        /*// 2.完成任务
        // 完成任务时设置流程变量
        Map<String, Object> paramMap = new HashMap<>();

        Holiday holiday = new Holiday();
        holiday.setDay(5d);
        holiday.setCreateTime(new Date());
        holiday.setName("5天请假流程");
        paramMap.put("item",holiday);
        // 设置不同节点办理人
        paramMap.put("user2","dept");
        paramMap.put("user3","money");
        paramMap.put("user4","general");
        taskService.complete(task.getId(),paramMap);*/



    }

    @Test
    void historyTaskQuery() {
        // 获取默认流程引擎
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();

        // 历史任务（已办任务）查询
        List<HistoricTaskInstance> historicTaskInstanceList = historyService.createHistoricTaskInstanceQuery().taskAssignee("one").list();
        historicTaskInstanceList.forEach(historicTaskInstance -> {
            System.out.println("任务名称：" + historicTaskInstance.getName());
            System.out.println("任务描述：" + historicTaskInstance.getDescription());
            System.out.println("任务持续时间：" + historicTaskInstance.getDurationInMillis() / 1000 / 60 / 60 + "小时");
            System.out.println("-------------");

        });
    }
}
